from flask import g, current_app
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from comment.modules.matched_result import Matched_result
from comment.modules import db
from financial.resources.transaction.serializer import MatchedResultPaginateSerializer
from datetime import datetime


class MatchedResultResource(Resource):
    """ 匹配结果列表的搜索查询功能 """

    def get(self):
        rp = RequestParser()
        rp.add_argument('start')
        rp.add_argument('end')
        rp.add_argument('curPage')
        rp.add_argument('perPage')

        args = rp.parse_args()

        cur_page = int(args.get('curPage'))
        per_page = int(args.get('perPage'))
        start = args.get('start')
        end = args.get('end')

        query = Matched_result.query
        if start and end:
            query = query.filter(db.cast(Matched_result.matchDate, db.DATE) >= db.cast(start, db.DATE)) \
                .filter(db.cast(Matched_result.matchDate, db.DATE) <= db.cast(end, db.DATE))

        result_list = query.paginate(cur_page, per_page, error_out=False)

        data = MatchedResultPaginateSerializer(result_list).to_dict()

        return {'msg': 'success', 'data': data}
